﻿Imports System.Data.SqlClient

Public Class Frm_Plainte
    Dim isDateValidate As Boolean = True
    Dim isDateValidat As Boolean = True
    Dim isUnique As Boolean = True
    Dim Date_Inscreption As String
    Dim Date_Hifde As String
    Public plnte As Plainte
    Public Sub selectYear()
        Dim myDate As Date = Date.Now
        Dim myYear As Integer = myDate.Year
        Comb_annee.SelectedItem = myYear
    End Sub
    Public Sub Remplir_list()
        Partager.moi.remplir_List_plainte()
        Partager.moi.remplir_List_client()
        Partager.moi.remplir_List_enemi()
        Dim clte As String
        Dim enem As String
        List_tous.Items.Clear()
        For Each pl As Plainte In Partager.moi.List_Plainte
            pl.remplir_Plainte_Ref()
            clte = ""
            enem = ""
            For Each ref As Plainte_Ref In pl.list_Plainte_Ref
                
                Dim cmd As New SqlClient.SqlCommand("select idAdv,idClt from refClt,refAdv" & _
                            " where refClt.idRef=refAdv.idRef and refClt.idRef=" & ref.ref, Partager.con)
                Partager.open()
                Partager.dr = cmd.ExecuteReader
                Partager.dr.Read()
                For Each clt As Client In Partager.moi.List_client
                    If Partager.dr("idClt") = clt.Id Then
                        If clt.Type = "ذاتي" Then
                            clte = clt.Nom & "  " & clt.Prenom
                        ElseIf clt.Type = "معنوي" Then
                            clte = clt.Nom_ent
                        End If
                        Exit For
                    End If

                Next
                For Each enm As Enemie In Partager.moi.List_enemi
                    If Partager.dr("idAdv") = enm.Id Then
                        If enm.Type = "ذاتي" Then
                            enem = enm.Nom & "  " & enm.Prenom
                        ElseIf enm.Type = "معنوي" Then
                            enem = enm.Nom_ent
                        End If
                        Exit For
                    End If

                Next
                Partager.dr.Close()
                Partager.close()


            Next
            List_tous.Items.Add(New ListViewItem(New String() {pl.Id, pl.Num_Plainte, pl.Tribunal, pl.Objecte, pl.Date_Inscreption, clte, enem, pl.dicision}))
        Next
    End Sub
    Private Sub Frm_Plainte_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolTip1.IsBalloon = True
        Comb_Type.SelectedIndex = 0

        For i As Integer = 1990 To 2020
            Comb_annee.Items.Add(i)
        Next
        selectYear()
        Partager.com_ville(Comb_mahkama, "Comb_mahkama", "mahkama")
        '----------------------------------------------------------
        Partager.remplirClient(Comb_nom)
        If Comb_nom.Items.Count > 0 Then
            Comb_nom.SelectedIndex = -1
        End If
        '---------------------------------------------------------
        Remplir_list()
    End Sub

    Private Sub txt_planite_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_planite.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Comb_dicision_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_dicision.SelectedIndexChanged
        If Comb_dicision.Text = "حفظ" Then
            date_hifd.Enabled = True
            btn_envois.Enabled = False

        Else
            date_hifd.Text = "  /  /"
            date_hifd.Enabled = False
            btn_envois.Enabled = True
        End If
    End Sub

    Private Sub date_inscrip_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles date_inscrip.MaskInputRejected

    End Sub

    Private Sub date_inscrip_TypeValidationCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.TypeValidationEventArgs) Handles date_inscrip.TypeValidationCompleted
        If (Not e.IsValidInput) Then
            If (Not e.IsValidInput) Then
                If date_inscrip.Text <> "  /  /" Then
                    Me.ToolTip1.ToolTipTitle = "تارخ خاطئ !"
                    Me.ToolTip1.Show("هل تسخر مني >_< أدخل التاريخ على هذا الشكل: السنة/الشهر/اليوم", Me.date_inscrip, 0, -50, 5000)
                    isDateValidate = False
                End If
            End If
            If date_inscrip.Text = "  /  /" Then
                isDateValidate = True
            End If
        Else
            isDateValidate = True
        End If
    End Sub

    Private Sub date_hifd_TypeValidationCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.TypeValidationEventArgs) Handles date_hifd.TypeValidationCompleted
        If (Not e.IsValidInput) Then
            If (Not e.IsValidInput) Then
                If date_hifd.Text <> "  /  /" Then
                    Me.ToolTip1.ToolTipTitle = "تارخ خاطئ !"
                    Me.ToolTip1.Show("هل تسخر مني >_< أدخل التاريخ على هذا الشكل: السنة/الشهر/اليوم", Me.date_hifd, 0, -50, 5000)
                    isDateValidat = False
                End If
            End If
            If date_hifd.Text = "  /  /" Then
                isDateValidat = True
            End If
        Else
            isDateValidat = True
        End If
    End Sub

    Private Sub Comb_nom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_nom.SelectedIndexChanged
        If Comb_nom.Text <> "" Then
            Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
            Partager.remplir_CombAdvrT(CInt(Partager.deClt.Key), Comb_adv)
        Else
            Comb_adv.Items.Clear()
            Comb_adv.Text = ""
        End If
    End Sub

    Private Sub Btn_parc_nom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_nom.Click
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_mowakil = "Frm_Ref"
        Frm_Rech_Clients.Show()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Partager.flag = True
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_adver = "Frm_Idafa_khossoum"
        Partager.moi.remplir_List_enemi()
        Frm_Rech_Adver.Show()
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        selectYear()
        Comb_dicision.SelectedIndex = -1
        Comb_nom.SelectedIndex = -1
        Comb_siffa.SelectedIndex = -1
        cmbCaracEnemie.SelectedIndex = -1
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة شكاية جديدة عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If txt_planite.Text <> "" And Comb_nom.Text <> "" And Comb_adv.Text <> "" Then
                    If MsgBox("هل تريد فعلا إضافة هذه الشكاية ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة شكاية") = MsgBoxResult.Yes Then

                       
                            If isDateValidate And isDateValidat Then
                                If date_inscrip.Text = "  /  /" Then
                                    Date_Inscreption = Nothing
                                Else
                                    Date_Inscreption = date_inscrip.Text
                                    Date_Inscreption = Partager.reverseString(Date_Inscreption)
                                End If
                                If date_hifd.Text = "  /  /" Then
                                    Date_Hifde = Nothing
                                Else
                                    Date_Hifde = date_hifd.Text
                                    Date_Hifde = Partager.reverseString(Date_Hifde)
                                End If
                                '--------------------------------------------------------------
                                Dim plnt As New Plainte(1, CInt(txt_planite.Text), CInt(Comb_annee.Text), Comb_Type.Text, txt_nomwakil.Text, Date_Inscreption, txt_object.Text, Comb_mahkama.Text, txt_observ.Text, Comb_dicision.Text)
                                Partager.moi.Ajouter_plainte(plnt)
                                '--------------------------------------------------------------
                                Dim lastItem As Integer = Partager.moi.List_Plainte.Count - 1
                                plnte = CType(Partager.moi.List_Plainte.Item(lastItem), Plainte)
                                '--------------------------------------------------------------
                                Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                            Dim plnref As New Plainte_Ref(plnte.Id, Partager.deClt.Key, cmbCaracEnemie.Text, Comb_siffa.Text)
                                plnte.Ajouter_Plainte_Ref(plnref)
                                '--------------------------------------------------------------
                                If Comb_dicision.Text = "حفظ" Then
                                    Dim hfd As New Hifde(plnte.Id, Date_Hifde)
                                    plnte.Ajouter_hifde(hfd)
                                End If
                                '--------------------------------------------------------------
                                Remplir_list()
                                '--------------------------------------------------------------
                                Partager.vider(Me)
                                Comb_dicision.SelectedIndex = -1
                                Comb_nom.SelectedIndex = -1
                                Comb_siffa.SelectedIndex = -1
                                cmbCaracEnemie.SelectedIndex = -1
                                selectYear()
                                '-------------------------------------------------------------

                                MsgBox("تم إضافة الشكاية بنجاح", MsgBoxStyle.Information, "نجاح")
                            Else
                                MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                            End If

                       

                    End If
                Else
                    MsgBox("عفوا، عليك إدخال البيانات", MsgBoxStyle.Critical, "خطأ")
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذه الشكاية ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف الشكاية") = MsgBoxResult.Yes Then
                    Partager.close()

                    Dim plnt As Plainte = Partager.moi.List_Plainte(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_plainte(plnt.Id)
                    '--------------------------------------------------------------
                    Remplir_list()
                    '--------------------------------------------------------------
                    Partager.vider(Me)
                    Comb_dicision.SelectedIndex = -1
                    Comb_nom.SelectedIndex = -1
                    Comb_siffa.SelectedIndex = -1
                    cmbCaracEnemie.SelectedIndex = -1
                    selectYear()
                    '--------------------------------------------------------------
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                End If
            Else
                MsgBox("اختر شكاية من القائمة", MsgBoxStyle.Information, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            Partager.moi.remplir_List_client()
            Partager.moi.remplir_List_enemi()
            Dim clte As String = ""
            Dim enem As String = ""
            For Each i As Integer In List_tous.SelectedIndices
                plnte = Partager.moi.List_Plainte(i)

                txt_planite.Text = plnte.Num_Plainte
                txt_nomwakil.Text = plnte.Nom_wakil
                txt_object.Text = plnte.Objecte
                date_inscrip.Text = plnte.Date_Inscreption
                txt_observ.Text = plnte.Observ
                Comb_Type.Text = plnte.Type
                Comb_annee.Text = plnte.Annee
                Comb_mahkama.Text = plnte.Tribunal
                Comb_dicision.Text = plnte.dicision
                plnte.remplir_Plainte_Ref()
                For Each ref As Plainte_Ref In plnte.list_Plainte_Ref

                    cmbCaracEnemie.Text = ref.sifa_enemi
                    Comb_siffa.Text = ref.sifa_client

                    Dim cmd As New SqlClient.SqlCommand("select idAdv,idClt from refClt,refAdv" & _
                                " where refClt.idRef=refAdv.idRef and refClt.idRef=" & ref.ref, Partager.con)
                    Partager.open()
                    Partager.dr = cmd.ExecuteReader
                    Partager.dr.Read()
                    For Each clt As Client In Partager.moi.List_client
                        If Partager.dr("idClt") = clt.Id Then
                            If clt.Type = "ذاتي" Then
                                clte = clt.Nom & "  " & clt.Prenom
                            ElseIf clt.Type = "معنوي" Then
                                clte = clt.Nom_ent
                            End If
                            Exit For
                        End If

                    Next
                    For Each enm As Enemie In Partager.moi.List_enemi
                        If Partager.dr("idAdv") = enm.Id Then
                            If enm.Type = "ذاتي" Then
                                enem = enm.Nom & "  " & enm.Prenom
                            ElseIf enm.Type = "معنوي" Then
                                enem = enm.Nom_ent
                            End If
                            Exit For
                        End If

                    Next
                    Partager.dr.Close()
                    Partager.close()
                    Exit For
                Next
                Comb_nom.Text = clte
                Comb_adv.Text = enem

                plnte.remplir_List_hifde()
                date_hifd.Text = "  /  /"
                For Each h As Hifde In plnte.list_hifde
                    date_hifd.Text = h.Date_Hifd
                    Exit For
                Next

            Next
            If List_tous.SelectedIndices.Count <= 0 Then
                Partager.vider(Me)
                selectYear()
                Comb_dicision.SelectedIndex = -1
                Comb_nom.SelectedIndex = -1
                Comb_siffa.SelectedIndex = -1
                cmbCaracEnemie.SelectedIndex = -1
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then

                If txt_planite.Text <> "" And Comb_nom.Text <> "" And Comb_adv.Text <> "" Then
                    If MsgBox("هل تريد فعلا تعديل هذه الشكاية ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعيل شكاية") = MsgBoxResult.Yes Then


                        If isDateValidate And isDateValidat Then
                            If date_inscrip.Text = "  /  /" Then
                                Date_Inscreption = Nothing
                            Else
                                Date_Inscreption = date_inscrip.Text
                                Date_Inscreption = Partager.reverseString(Date_Inscreption)
                            End If
                            If date_hifd.Text = "  /  /" Then
                                Date_Hifde = Nothing
                            Else
                                Date_Hifde = date_hifd.Text
                                Date_Hifde = Partager.reverseString(Date_Hifde)
                            End If
                            '--------------------------------------------------------------
                            Dim plnt As New Plainte(1, CInt(txt_planite.Text), CInt(Comb_annee.Text), Comb_Type.Text, txt_nomwakil.Text, Date_Inscreption, txt_object.Text, Comb_mahkama.Text, txt_observ.Text, Comb_dicision.Text)
                            Partager.moi.modifier_plainte(plnt, plnte.Id)
                            '--------------------------------------------------------------
                            plnte.suprimmer_Plainte_Ref()
                            Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                            Dim plnref As New Plainte_Ref(plnte.Id, Partager.deClt.Key, cmbCaracEnemie.Text, Comb_siffa.Text)
                            plnte.Ajouter_Plainte_Ref(plnref)
                            '--------------------------------------------------------------
                            plnte.suprimmer_hifde()
                            If Comb_dicision.Text = "حفظ" Then
                                Dim hfd As New Hifde(plnte.Id, Date_Hifde)
                                plnte.Ajouter_hifde(hfd)
                            End If
                            '--------------------------------------------------------------
                            Remplir_list()
                            '--------------------------------------------------------------
                            Partager.vider(Me)
                            date_hifd.Text = "  /  /"
                            Comb_dicision.SelectedIndex = -1
                            Comb_nom.SelectedIndex = -1
                            Comb_siffa.SelectedIndex = -1
                            cmbCaracEnemie.SelectedIndex = -1
                            selectYear()
                            '-------------------------------------------------------------

                            MsgBox("تم تعديل الشكاية بنجاح", MsgBoxStyle.Information, "نجاح")
                        Else
                            MsgBox("إن هذا التاريخ الذي أدخلت خاطئ", MsgBoxStyle.Critical, "خطأ")
                        End If



                    End If
                Else
                    MsgBox("عفوا، عليك إدخال البيانات", MsgBoxStyle.Critical, "خطأ")
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Frm_Rech_Plainte.Show()
    End Sub

    Private Sub txt_planite_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_planite.TextChanged

    End Sub

    Private Sub btn_envois_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_envois.Click
        If List_tous.SelectedItems.Count > 0 Then
            Me.Enabled = False
            Frm_Envois.Show()
        Else
            MsgBox("اختر الشكاية التي تريد إرسالها", MsgBoxStyle.Critical, "خطأ")
        End If
    End Sub
End Class